//
//  E_ReaderViewController.m
//  E_Reader
//
//  Created by 阿虎 on 14-8-8.
//  Copyright (c) 2014年 tiger. All rights reserved.
//

#import "E_ReaderViewController.h"
#import "E_ReaderView.h"
#import "E_CommonManager.h"
#import "JSTAdWebViewController.h"

#define MAX_FONT_SIZE 27
#define MIN_FONT_SIZE 17

static int advertIntervalTime = 0;

@interface E_ReaderViewController ()<E_ReaderViewDelegate>
{
    E_ReaderView *_readerView;
    
}

@property (nonatomic, strong) NSTimer *advertIntervalTimer;

@property (nonatomic, strong) NSDictionary *bannerDict;

@end

@implementation E_ReaderViewController

- (void)dealloc
{
    [_advertIntervalTimer invalidate];
    _advertIntervalTimer = nil;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self createContent];
    
    [self createBannerAdvert];
}

- (void) createBannerAdvert
{
    if (IsStrEmpty([JSTSystemInfo bannerId])) {
        return;
    }
    [JSTServer requestAdvert:[JSTSystemInfo bannerId] dataDic:^(NSDictionary *dataDic) {
        _bannerDict = dataDic;
        NSString *imgUrl = [[dataDic valueForKey:@"data"] valueForKey:@"imageurl"];
        [_advertView sd_setImageWithURL:[NSURL URLWithString:imgUrl]];
        [_advertIntervalTimer invalidate];
        _advertIntervalTimer = nil;
        [self createAdvertIntervalTimer];
    } errorStr:^(NSString *error, NSInteger status) {
        
    }];
}

- (void) createAdvertIntervalTimer
{
    _advertIntervalTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(advertInvterval) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:_advertIntervalTimer forMode:NSDefaultRunLoopMode];
}

- (void) advertInvterval
{
    advertIntervalTime ++;
    if (advertIntervalTime == [JSTSystemInfo advertInterval]) {
        advertIntervalTime = 0;
        [self createBannerAdvert];
    }
}

- (void) createAdvert
{
    _advertView = [[UIImageView alloc] init];
    _advertView.frame = CGRectZero;
    [self.view addSubview:_advertView];
}

- (void) createContent
{
    _advertView = [[UIImageView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:_advertView];
    
    _readerView = [[E_ReaderView alloc] initWithFrame:CGRectMake(offSet_x, offSet_y, kDeviceWidth - 2*offSet_x, kDeviceHeight - 2*offSet_y)];
    _readerView.keyWord = _keyWord;
    _readerView.magnifiterImage = _themeBgImage;
    _readerView.delegate = self;
    [self.view addSubview:_readerView];
    
    if (IsStrEmpty([JSTSystemInfo bannerStatus])) {
        return;
    }
    if ([JSTSystemInfo bannerStatus].intValue == 0) {
        _advertView.frame = CGRectMake(0, 0, kDeviceWidth, kCurrentScreenWidth(75));
        _readerView.frame = CGRectMake(offSet_x, CGRectGetMaxY(_advertView.frame) + 10, kDeviceWidth - 2 * offSet_x , kDeviceHeight - offSet_y - kCurrentScreenWidth(75));
    }else{
        _advertView.frame = CGRectMake(0, kDeviceHeight - kCurrentScreenWidth(75) - 10, kDeviceWidth, kCurrentScreenWidth(75));
        _readerView.frame = CGRectMake(offSet_x, offSet_y, kDeviceWidth - 2*offSet_x, kDeviceHeight - offSet_y - kCurrentScreenWidth(75));
    }
    _advertView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(advertChange)];
    [_advertView addGestureRecognizer:tap];
}

- (void) advertChange
{
    if (!_bannerDict) {
        return;
    }
    NSDictionary *dic = [_bannerDict valueForKey:@"data"];
    
    NSString *url = [dic valueForKey:@"apkurl"];
    JSTHomePageModel *model = kHomePageModel;
    model.isRead = YES;
    [model createOtherAdvertOclick:dic];
    [_delegate changeOtherPage:url];
    
}

#pragma mark - ReaderViewDelegate
- (void)shutOffGesture:(BOOL)yesOrNo{
    [_delegate shutOffPageViewControllerGesture:yesOrNo];
}

- (void)ciBa:(NSString *)ciBaString{

    [_delegate ciBaWithString:ciBaString];
}

- (void)hideSettingToolBar{
    [_delegate hideTheSettingBar];
}

- (void)setFont:(NSUInteger )font_
{
    _readerView.font = font_;
}

- (void)setText:(NSString *)text
{
    _text = text;
    _readerView.text = text;
   
    [_readerView render];
}

- (NSUInteger )font
{
    return _readerView.font;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (CGSize)readerTextSize
{
    return _readerView.bounds.size;
}
@end
